package com.xcity.game.battle.pve.template;

import java.util.ArrayList;
import java.util.List;

import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.item.drop.Drop;
import com.xcity.game.item.drop.impl.ExpDrop;
import com.xcity.game.item.drop.impl.GoldDrop;
import com.xcity.game.item.drop.impl.ItemDrop;
import com.xcity.game.npc.NpcTemplate;

import naga.x.App;
import naga.x.game.template.Template;
import naga.x.game.template.TemplateException;
import naga.x.game.template.TemplateService;
import naga.x.game.template.annotation.Excel;
import naga.x.util.NumberUtils;
import naga.x.util.StringUtils;

/**
 * 资源副本
 * @author shipeng.zhu
 *
 */
@Excel(value = "resstage.xlsx", sheetName = "资源副本")
public class ResStageTemplate extends Template {

	/** 关卡名称 */
	protected String name;

	/** 描述 */
	protected String desc;

	/** 类型 */
	protected byte type;

	/** 准入等级 */
	protected int levelLimit;

	@Deprecated
	/** 耐力消耗 */
	protected int costStr;
	
	/** 小怪id */
	protected int[] npcIds;

	/** bossid */
	protected int bossId;

	/** 剩余血量 */
	protected int hpStar;

	/** 死亡数量 */
	protected int deathStar;

	/** 回合数 */
	protected int roundStar;

	/** 时间(秒) */
	protected int timeLimit;

	/** 胜利奖励 */
	protected String[] rewards;

	/** 选关图标 */
	protected String icon;

	/** 背景图片 */
	protected String bgImage;

	/** 神秘商店出现概率 */
	protected int showShop;

	protected NpcTemplate[] npcs;
	protected NpcTemplate boss;
	protected List<Drop> rewardsDrop;
	protected int previousIndex;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public byte getType() {
		return type;
	}

	public void setType(byte type) {
		this.type = type;
	}

	public int getLevelLimit() {
		return levelLimit;
	}

	public void setLevelLimit(int levelLimit) {
		this.levelLimit = levelLimit;
	}

	@Deprecated
	public int getCostStr() {
		return costStr;
	}

	@Deprecated
	public void setCostStr(int costStr) {
		this.costStr = costStr;
	}

	public int[] getNpcIds() {
		return npcIds;
	}

	public void setNpcIds(int[] npcIds) {
		this.npcIds = npcIds;
	}

	public int getBossId() {
		return bossId;
	}

	public void setBossId(int bossId) {
		this.bossId = bossId;
	}

	public int getHpStar() {
		return hpStar;
	}

	public void setHpStar(int hpStar) {
		this.hpStar = hpStar;
	}

	public int getDeathStar() {
		return deathStar;
	}

	public void setDeathStar(int deathStar) {
		this.deathStar = deathStar;
	}

	public int getRoundStar() {
		return roundStar;
	}

	public void setRoundStar(int roundStar) {
		this.roundStar = roundStar;
	}

	public int getTimeLimit() {
		return timeLimit;
	}

	public void setTimeLimit(int timeLimit) {
		this.timeLimit = timeLimit;
	}

	public String[] getRewards() {
		return rewards;
	}

	public void setRewards(String[] rewards) {
		this.rewards = rewards;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getBgImage() {
		return bgImage;
	}

	public void setBgImage(String bgImage) {
		this.bgImage = bgImage;
	}

	public int getShowShop() {
		return showShop;
	}

	public void setShowShop(int showShop) {
		this.showShop = showShop;
	}
	
	public void setRewardsDrop(List<Drop> rewardsDrop) {
		this.rewardsDrop = rewardsDrop;
	}

	public List<Drop> getRewardsDrop() {
		return rewardsDrop;
	}

	public int getPreviousIndex() {
		return previousIndex;
	}

	public void setPreviousIndex(int previousIndex) {
		this.previousIndex = previousIndex;
	}

	@Override
	public void validate() throws TemplateException {
		try {
			rewardsDrop = parseDrops(rewards, true)[0];
			rewards = null;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("stage.xlsx配置有误! id=" + id);
		}
	}

	public NpcTemplate[] getNpcs() {
		if(npcs == null && npcIds != null){
			npcs = new NpcTemplate[npcIds.length];
			TemplateService service = App.getApp().getServiceManager().get(TemplateService.class);
			for(int i = 0; i < npcIds.length; i++){
				NpcTemplate npc = service.get(NpcTemplate.class, npcIds[i]);
				npcs[i] = npc;
			}
		}
		return npcs;
	}

	public NpcTemplate getBoss() {
		return boss != null || bossId == 0? boss :(boss = App.getApp().getServiceManager().get(TemplateService.class).get(NpcTemplate.class, bossId));
	}

	static List<Drop>[] parseDrops(String[] rewards, boolean hasProbability) {
//		List<List<Drop>> drops = new ArrayList<List<Drop>>();
//		for (int i = 0; i < rewards.length; i += 3) {
//			// 每3个为一组(现金、经验、物品)
//			List<Drop> group = new ArrayList<Drop>();
//			drops.add(group);
//			int gold = NumberUtils.parseInt(rewards[i]);
//			group.add(new GoldDrop(gold));
//			
//			if (i + 1 >= rewards.length) {
//				break;
//			}
//			int exp = NumberUtils.parseInt(rewards[i + 1]);
//			group.add(new ExpDrop(exp));
//			
//			if (i + 2 >= rewards.length) {
//				break;
//			}
//			String items = rewards[i + 2];
//			if (StringUtils.isNotEmpty(items)) {
//				String[] strs = StringUtils.split(items, ";");
//				for (String s : strs) {
//					int dot = s.indexOf('=');
//					int id = NumberUtils.parseInt(s.substring(0, dot));
//					String suffix = s.substring(dot + 1);
//					dot = suffix.indexOf(',');
//					int count, probability;
//					if (hasProbability || dot != -1) {
//						probability = NumberUtils.parseInt(suffix.substring(0, dot));
//						count = NumberUtils.parseInt(suffix.substring(dot + 1));
//					} else {
//						probability = GameStaticConfig.defaultChanceBase;
//						count = NumberUtils.parseInt(suffix);
//					}
////					if (dot < 0) {
////						probability = GameStaticConfig.defaultChanceBase;
////						count = NumberUtils.parseInt(suffix);
////					} else {
////						probability = NumberUtils.parseInt(suffix.substring(0, dot));
////						count = NumberUtils.parseInt(suffix.substring(dot + 1));
////					}
//					group.add(new ItemDrop(probability, id, count));
//				}
//			}
//		}
		List<List<Drop>> drops = new ArrayList<List<Drop>>();
		List<Drop> group = new ArrayList<Drop>();
		drops.add(group);
		int size = rewards.length;
		if(size > 0){
			int gold = NumberUtils.parseInt(rewards[0]);
			group.add(new GoldDrop(gold));
		}
		if(size > 1){
			int exp = NumberUtils.parseInt(rewards[1]);
			group.add(new ExpDrop(exp));
		}
		if(size > 2){
			for (int i = 2; i < size; i++) {
				int dot = rewards[i].indexOf('=');
				int id = NumberUtils.parseInt(rewards[i].substring(0, dot));
				String[] subStrs = StringUtils.split(rewards[i].substring(dot + 1), ",");
				int count, count2, probability;
				int lenth = subStrs.length;
				if(lenth == 3){
					probability = NumberUtils.parseInt(subStrs[0]);
					count = NumberUtils.parseInt(subStrs[1]);
					count2 = NumberUtils.parseInt(subStrs[2]);
				}else if(lenth == 2){
					if(hasProbability){
						probability = NumberUtils.parseInt(subStrs[0]);
						count = NumberUtils.parseInt(subStrs[1]);
						count2 = count;
					}else{
						probability = GameStaticConfig.defaultChanceBase;
						count = NumberUtils.parseInt(subStrs[0]);
						count2 = NumberUtils.parseInt(subStrs[1]);
					}
				}else{
					probability = GameStaticConfig.defaultChanceBase;
					count = NumberUtils.parseInt(subStrs[0]);
					count2 = count;
				}
				group.add(new ItemDrop(probability, 0, id, count, count2));
			}
		}
		return drops.toArray(new List[drops.size()]);
	}
}
